var app = getApp();
Page({
  data: {
    giveInfo: null,
    userId: ''
  },

  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
      userId: app.globalData.user.plainText
    })
    this.getGiveInfo(options.id);
  },
  getGiveInfo: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getGiveInfo',
      data: {
        id: id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var giveInfo = r.data.data;
          giveInfo.images = [];
          giveInfo.video = null;
          for (var i of giveInfo.resourceList) {
            var s = i.size;
            if (s == null) {
              giveInfo.images.push(i);
            } else {
              giveInfo.video = i;
            }
          }
          giveInfo.adoptClaimList = giveInfo.adoptClaim.split(',');
          that.setData({
            giveInfo: giveInfo
          })
        } else {
          wx.showToast({
            title: '获取送养信息失败',
            icon: 'none',
            duration: 30000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取送养信息失败',
          icon: 'none',
          duration: 30000
        })
      }
    })
  },
  //返回
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  //进入聊天
  chatInfo: function() {
    var giveInfo = this.data.giveInfo;
    var senderId = giveInfo.userId;
    var consigneeId = this.data.userId;
    var chat = null;
    for (var i of app.globalData.chat) {
      if (i.id == senderId + consigneeId || i.id == consigneeId + senderId) {
        chat = i;
      }
    }
    if (chat == null) {
      var chat = {
        id: giveInfo.userId + this.data.userId,
        userId: giveInfo.userId,
        nikeName: giveInfo.nikeName,
        headPortrait: giveInfo.headPortrait,
        chatList: []
      }
      app.globalData.chat.push(chat);
    }
    wx.navigateTo({
      url: '/pages/chatInfo/chatInfo?id=' + chat.id
    })
  },
  //  视频进入全屏
  videoScreen: function(e) {
    var fullScreen = e.detail.fullScreen;
    if (fullScreen) {
      this.setData({
        fullScreen: true
      })
    } else {
      this.setData({
        fullScreen: false
      })
    }
  },
  //查看他的信息
  readUser: function() {
    var userId = this.data.giveInfo.userId;
    var plainText = app.globalData.user.plainText;
    if (userId == plainText) {
      return;
    }
    wx.navigateTo({
      url: '/pages/readUser/readUser?id=' + userId
    })
  }
})